package com.sophialex.health.record.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.eclipse.swt.widgets.DateTime;

import com.sophialex.health.system.frame.util.RefMethod;

public class PublicMethod {
	static SimpleDateFormat sf;
	static Date date;
	private static int index;
	private static String kg = " ";

	public static String dateIime = "yyyy-MM-dd HH:mm:ss";
	public static String dates = "yyyy-MM-dd";
	public static String time = "HH:mm:ss";
	public static String MD = "MM-dd";

	public static String ChangeTime(String dateString, String type) {
		sf = new SimpleDateFormat(type);
		try {
			date = sf.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return sf.format(date);

	}

	public static String ChangeTime(Date date, String type) {
		sf = new SimpleDateFormat(type);
		if (date != null)
			return sf.format(date);
		else
			return "";
	}

	public static String changeStrofLength(String str, int length) {
		if (str.length() < length) {
			index = length - str.length();
			for (int i = 0; i < index; i++) {
				str = str + kg;
			}
			return str;
		} else
			return str;

	}

	public static String getDate(DateTime dateTime) {
		sf = new SimpleDateFormat(dates);
		try {
			return sf.format(sf.parse(dateTime.getYear() + "-"
					+ (dateTime.getMonth() + 1) + "-" + dateTime.getDay()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	public static String getTime(DateTime dateTime) {
		sf = new SimpleDateFormat(time);
		try {
			return sf.format(sf.parse(dateTime.getHours() + ":"
					+ dateTime.getMinutes() + ":" + dateTime.getSeconds()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	public static void setDate(DateTime dateTime, Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		dateTime.setDate(calendar.get(Calendar.YEAR), calendar
				.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
	}

	public static void setTime(DateTime dateTime, Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		dateTime.setTime(calendar.get(Calendar.HOUR_OF_DAY), calendar
				.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
	}

	/** 排序 */
	public static <T> void storeList(List<T> list, final String[] propertys) {
		if (list == null)
			return;
		Collections.sort(list, new Comparator<T>() {
			Object p0, p1;

			public int compare(T arg0, T arg1) {
				p0 = "";
				p1 = "";
				p0 = RefMethod.invokeGetMethod(arg0, RefMethod
						.gainGetMethod(propertys[0]));
				p1 = RefMethod.invokeGetMethod(arg1, RefMethod
						.gainGetMethod(propertys[0]));
				return (p0 == null ? "" : p0).toString().compareTo(
						p1 == null ? "" : p1.toString());
			}
		});
	}
}
